<template>
  <div>
    <h1 style="font-size: 28px">修改设备</h1>
    <div>
      <a-form :label-col="{ span: 4 }" :wrapper-col="{ span: 14 }">
        <a-form-item label="设备ID">
          <a-input :value="id" disabled/>
        </a-form-item>
        <a-form-item label="MAC地址" required>
          <a-input v-model:value="mac_address"/>
        </a-form-item>
        <a-form-item label="站点ID">
          <a-input :value="site_id" disabled/>
        </a-form-item>
        <a-form-item label="设备编号">
          <a-input :value="device_no" disabled/>
        </a-form-item>
        <a-form-item label="描述">
          <a-textarea v-model:value="intro"/>
        </a-form-item>
        <a-form-item :wrapper-col="{ span: 14, offset: 4 }">
          <a-button type="primary" style="width: 140px;" @click="onConfirm">确定</a-button>
          <a-button style="width: 140px;margin-left: 20px;" @click="onBack">返回</a-button>
        </a-form-item>
      </a-form>
    </div>
  </div>
</template>

<script lang="ts">

import {defineComponent, reactive, toRefs} from "vue";
import {message} from "ant-design-vue";
import {modifyDevice} from "/@/api/product/device";
import {HttpResult} from "/@/api/core/model/coreModel";

export default defineComponent({
  name: 'DeviceEdit',
  props: {
    item: {
      type: Object,
      default: null,
    },
    viewType: {
      type: String,
      default: null,
    }
  },
  setup(props, {emit}) {
    const data = reactive({
      id: props.item.id,
      device_no: props.item.device_no,
      site_id: props.item.site_id,
      mac_address: props.item.mac_address,
      intro: props.item.intro,
    });

    const onConfirm = () => {
      if (data.mac_address.length != 12) {
        message.warn("请输入有效的MAC地址");
        return;
      }
      modifyDevice({
        id: data.id,
        mac_address: data.mac_address,
        intro: data.intro,
      }).then((result: HttpResult) => {
        if (result.code == 0) {
          message.success("修改成功");
          emit("my-emit", 'Confirm');
        } else {
          message.error(result.message);
        }
      });
    }

    const onBack = () => {
      emit("my-emit", 'Cancel');
    }

    return {
      ...toRefs(data),
      onConfirm,
      onBack
    };
  }
});

</script>

<style scoped>

</style>
